package com.study.springboot.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wei.zhao
 * @description: 基于FastJson的工具类
 * @date 2021年12月06日 15:26
 */
public class JsonUtil {

    private final static Logger logger = LoggerFactory.getLogger(JsonUtil.class);

    private static SerializeConfig config;

    static {
        config = new SerializeConfig();
        config.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
    }

    private static final SerializerFeature[] features = {
            SerializerFeature.DisableCircularReferenceDetect,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullNumberAsZero,
            SerializerFeature.WriteNullStringAsEmpty
    };

    /**
     * 对象转Json
     */
    public static String toJSON(Object object) {
        try {
            return JSON.toJSONString(object, config, features);
        } catch (Exception e) {
            logger.error("对象转Json失败:{}, data:{}", e.getMessage(), object);
        }
        return null;
    }

    /**
     * Json转对象
     */
    public static <T> T toBean(String json, Class<T> clazz) {
        try {
            return JSON.parseObject(json, clazz);
        } catch (Exception e) {
            logger.error("Json转对象失败:{}, data:{}", e.getMessage(), json);
        }
        return null;
    }

    /**
     * Json转对象，嵌套泛型
     */
    public static <T> T toBean(String json, TypeReference<T> typeReference) {
        try {
            T t = JSON.parseObject(json, typeReference);
            return t;
        } catch (Exception e) {
            logger.error("Json转带泛型对象失败:{}, data:{}", e.getMessage(), json);
        }
        return null;
    }

    /**
     * Json转对象集合
     */
    public static <T> List<T> toList(String json, Class<T> clazz) {
        try {
            return JSON.parseArray(json, clazz);
        } catch (Exception e) {
            logger.error("Json转对象集合失败:{}, data:{}", e.getMessage(), json);
        }
        return null;
    }

    /**
     * Json转Object数组
     */
    public static Object[] toArray(String json) {
        try {
            return toArray(json, null);
        } catch (Exception e) {
            logger.error("Json转Object数组失败:{}, data:{}", e.getMessage(), json);
        }
        return null;
    }

    /**
     * Json转对象数组
     */
    public static <T> Object[] toArray(String json, Class<T> clazz) {
        try {
            return JSON.parseArray(json, clazz).toArray();
        } catch (Exception e) {
            logger.error("Json转对象数组失败:{}, data:{}", e.getMessage(), json);
        }
        return null;
    }

    /**
     * Json转Map
     */
    public static Map<?, ?> toMap(String json) {
        try {
            return JSON.parseObject(json);
        } catch (Exception e) {
            logger.error("Json转Map失败:{}, data:{}", e.getMessage(), json);
        }
        return null;
    }

    /**
     * 从json字符串获取指定key的字符串
     */
    public static Object getValueFromJson(final String json, final String key) {
        try {
            if (StringUtils.isAnyBlank(json, key)) {
                return null;
            }
            return JSON.parseObject(json).getString(key);
        } catch (Exception e) {
            logger.error("从json字符串获取指定key的字符串失败:{}, data:{}, key:{}", e.getMessage(), json, key);
        }
        return null;
    }

}
